package com.neusoft.hifly.rpc.area.dao.fallback;

import com.neusoft.hifly.core.log.Log;
import com.neusoft.hifly.core.pojo.ApiCode;
import com.neusoft.hifly.core.pojo.ApiResponse;
import com.neusoft.hifly.core.utils.GlobalConstants;
import com.neusoft.hifly.rpc.area.dao.ICityDao;
import com.neusoft.hifly.rpc.area.dao.IProvDao;
import com.neusoft.hifly.rpc.area.entity.CityObj;
import com.neusoft.hifly.rpc.area.entity.ProvObj;
import feign.hystrix.FallbackFactory;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * Hystrix 熔断
 */
@Component
public class CityFallback implements FallbackFactory<ICityDao> {

	@Override
	public ICityDao create(final Throwable throwable) {
		final String msg = throwable == null ? "" : throwable.getMessage();
		if (!StringUtils.isEmpty(msg)) {
			Log.error(msg);
		}
		return new ICityDao() {
			/**
			 * 根据id查询对象
			 *
			 * @param logicId id
			 * @return 对象
			 */
			@Override
			public ApiResponse<?> getByLogicId(long logicId) {
				return new ApiResponse<>(ApiCode.ERROR.getCode(), GlobalConstants.FEIGN_ERROR);
			}

			/**
			 * 查询所有对象
			 *
			 * @return 对象集合
			 */
			@Override
			public <T extends List<CityObj>> ApiResponse<T> findAll() {
				return new ApiResponse<>(ApiCode.ERROR.getCode(), GlobalConstants.FEIGN_ERROR);
			}

			/**
			 * 通过省份查询城市列表
			 *
			 * @param provLogicId 省份逻辑ID
			 * @return 集合
			 */
			@Override
			public <T extends List<CityObj>> ApiResponse<T> findByProvLogicId(long provLogicId) {
				return new ApiResponse<>(ApiCode.ERROR.getCode(), GlobalConstants.FEIGN_ERROR);
			}
		};
	}

}
